package com.android.phoenix.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.Toast;

import com.android.phoenix.R;
import com.dfzy.android.model.IHandleHolder;
import com.dfzy.android.model.IHandler;
import com.dfzy.android.ui.tab.AnimationTabHost;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements IHandler {

	private AnimationTabHost tabHost;
	private long back = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_main);
		createUI();
	}

	private void createUI() {
		LayoutInflater inflater = LayoutInflater.from(this);
		tabHost = (AnimationTabHost) findViewById(android.R.id.tabhost);
		tabHost.setup();
		// 创建选项卡
		TabHost.TabSpec spec = tabHost.newTabSpec("tab1");
		spec.setIndicator(inflater.inflate(R.layout.tab_btn_1, null))
				.setContent(new Intent(this, MusicActivity.class));
//		MainActivity.this.startActivity(new Intent(MainActivity.this,
//				MusicActivity.class));
		// Thread musicThread = new Thread(new Runnable() {
		// @Override
		// public void run() {
		// Intent musicIntent = new Intent(MainActivity.this,
		// MusicPlayActivity.class);
		// MainActivity.this.startActivity(musicIntent);
		// }
		// });
		// musicThread.start(); // 启动线程
		tabHost.addTab(spec);

		spec = tabHost.newTabSpec("tab2");
		spec.setIndicator(inflater.inflate(R.layout.tab_btn_2, null))
				.setContent(new Intent(this, HealthActivity.class));
		tabHost.addTab(spec);

		spec = tabHost.newTabSpec("tab3");
		spec.setIndicator(inflater.inflate(R.layout.tab_btn_3, null))
				.setContent(new Intent(this, SportActivity.class));
		tabHost.addTab(spec);

		spec = tabHost.newTabSpec("tab4");
		spec.setIndicator(inflater.inflate(R.layout.tab_btn_4, null))
				.setContent(new Intent(this, DiscoveryActivity.class));
		tabHost.addTab(spec);

		spec = tabHost.newTabSpec("tab5");
		spec.setIndicator(inflater.inflate(R.layout.tab_btn_5, null))
				.setContent(new Intent(this, MyInfoActivity.class));
		tabHost.addTab(spec);

		tabHost.setCurrentTab(2);
	}

	@Override
	public void handleMsg(Message msg) {

	}

	@Override
	public IHandleHolder getHolder() {
		// return (IHandleHolder) getApplication();
		return null;
	}

	protected void onDestroy() {
		// getHolder().removeHandler(this.getClass().getSimpleName());
		super.onDestroy();
	}

	protected Handler h = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == FINISH) {
				finish();
			} else {
				handleMsg(msg);
			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			System.out.println("======================");
			long current = System.currentTimeMillis();
			if ((current - back) < 2000) {
				finish();
				System.exit(0);
			} else {
				back = current;
				Toast.makeText(MainActivity.this, "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}